package com.llt.system.service.user.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.llt.common.core.enums.ResultCode;
import com.llt.common.security.exception.ServiceException;
import com.llt.system.domain.user.dto.UserDTO;
import com.llt.system.domain.user.dto.UserQueryDTO;
import com.llt.system.domain.user.po.User;
import com.llt.system.domain.user.vo.UserVO;
import com.llt.system.magger.UserCacheManager;
import com.llt.system.mapper.user.UserMapper;
import com.llt.system.service.user.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author llt
 * @since 2024-10-21
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper userMapper;

    private final UserCacheManager userCacheManager;

    @Override
    public List<UserVO> list(UserQueryDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return userMapper.selectUserList(dto);
    }

    @Override
    public int updateStatus(UserDTO dto) {
        // 1. 拿到该用户的信息
        User user = userMapper.selectById(dto.getUserId());
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        // 2. 修改状态
        user.setStatus(dto.getStatus());
        userCacheManager.updateStatus(user.getUserId(), dto.getStatus());
        return userMapper.updateById(user);
    }
}
